<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class upload_library
{
	function __construct()
	{
		$this->CI = get_instance();
	}     
	
	/*
	 * @method: uploadfile
	 * @param : string $path
	 * @date  : 06/04/2013
	 * @desc  : upload hinh anh
	 * @email : tritigi@gmail.com
	 */

	function uploadfile($path='../uploads/images/')
	{
		if(is_uploaded_file($_FILES['filedata']['tmp_name']))
		{
			$filename = stripslashes($_FILES['filedata']['name']);
			$path_info = pathinfo($filename); 
			$extension = strtolower($path_info['extension']) ;
			
			$name = $this->kodau($path_info['filename']).'_'.date("dmYHis").'.'.$extension ;          
			$filename = $path. $name ;
			if(move_uploaded_file($_FILES['filedata']['tmp_name'], $filename)){
				return $name.'*'.$filename.'*'.$extension;
			}else{
				return false;  
			}              
		}else{
			return false; 
		}
	} 
	/*
	 * @method: kodau
	 * @param : string $str
	 * @date  : 06/04/2013
	 * @desc  : chuyễn chuổi co dau thanh khong dau, cac ky tu đăc biệt thành dấu gạch nối (_)
	 * @email : tritigi@gmail.com
	 */
	function kodau($str)
	{
		$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
		$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
		$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
		$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
		$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
		$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
		$str = preg_replace("/(đ)/", 'd', $str);
		$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
		$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
		$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
		$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
		$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
		$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
		$str = preg_replace("/(Đ)/", 'D', $str);
		$str = preg_replace("/( )/", '-', $str);
		$str = preg_replace("/(\'|\"|`|&|,|\.|\?)/", '', $str);
		$str = preg_replace("/(---|--)/", '-', $str);
		
		$pattern = '/([^a-z0-9\-\._])/i';
		$str = preg_replace($pattern, '', $str);
			
		$str = strtolower($str);
		return $str;
	}
}  
?>
